package com.zzj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzj.entity.Notice;
import com.zzj.mapper.NoticeMapper;
import com.zzj.service.INoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzj.service.RedisService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 公告 服务实现类
 * </p>
 *
 * @author zzj
 * @since 2023-05-13
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Resource
    private RedisService redisService;
    @Override
    public int getNoticeNoReadNum(String username) {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Notice::getStatus, 1)
                .and(i -> {
                    i.eq(Notice::getReceiveUsername, "")
                            .or()
                            .eq(Notice::getReceiveUsername, username);
                });
        long count = this.count(queryWrapper);
        String noticeReadId = redisService.getNoticeReadId(username);
        if(noticeReadId!=null){
            int length = noticeReadId.split(",").length;
            count-=length;
        }
        return Integer.parseInt(String.valueOf(count));
    }

    @Override
    public Notice find(int id) {
        return this.getOne(Wrappers.<Notice>lambdaQuery()
                .eq(Notice::getId,id));
    }
}
